import article from '../../api/article'
import * as types from '../mutation-type'

const state = {
	all: [],
}

const getters = {
	allArticles: state => state.all,
	count: state => state.all.length
}

const actions = {
	getAllArticles({commit}, param) {
		console.log('invoke actions getAllArticles')
		article.getArticles(param.page, articles => {
			console.log('articles', articles)
			commit(types.RECEIVE_ARTICLES, articles);
		})
	}
}

const mutations = {
	[types.RECEIVE_ARTICLES] (state, articles) {
		console.log('mutations RECEIVE_ARTICLES')
		state.all = articles;
		state.count = articles.length
	},
	[types.ADD_TO_HISTORY] (state, {id}) {
		state.all.find(a => a.id === id).enabled = false;
	},
	[types.ENABLE_ARTICLE] (state, {id}) {
		console.log('mutations ENABLE_ARTICLE', id)
		state.all.find(a => a.id === id).enabled = true;
	}
}

export default {
	state,
	getters,
	actions,
	mutations
}